package com.xh.core.util;

import com.aspose.words.Font;
import com.aspose.words.INodeChangingCallback;
import com.aspose.words.NodeChangingArgs;
import com.aspose.words.NodeType;
import com.aspose.words.Run;
import com.xh.tools.question.constant.QuestionConstant;

import java.awt.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class HandleNodeChangingFontChanger implements INodeChangingCallback {
    private double conSize;

    public  HandleNodeChangingFontChanger(double conSize) {
        this.conSize = conSize;
    }

    @Override
    public void nodeInserting(NodeChangingArgs nodeChangingArgs) throws Exception {

    }

    @Override
    public void nodeInserted(NodeChangingArgs args) throws Exception {
        if (args.getNode().getNodeType() == NodeType.RUN) {
            Run run = (Run) args.getNode();
            Font font = run.getFont();
            // 如果是英文，字体设置为 Times New Roman吗，否则字体设置为宋体
            if (RegexUtils.Match(run.getText(), "[a-zA-Z]")) {
                font.setName("Times New Roman");
            } else {
                font.setName("宋体");
            }
            // 处理试卷大标题的字号
            if (run.getText().startsWith("##") && run.getText().endsWith("##")) {
                run.setText(run.getText().substring(2, run.getText().length() - 2));
                font.setSize(16);
            }else if (RegexUtils.Match(HtmlUtils.recoverHtmlChar(run.getText()), QuestionConstant.REGEX_MATCH_RULE.QUESTION_TYPE_START)) {
                font.setSize(12);
            } else {
                font.setSize(conSize);
            }
        }
    }

    @Override
    public void nodeRemoving(NodeChangingArgs nodeChangingArgs) throws Exception {

    }

    @Override
    public void nodeRemoved(NodeChangingArgs nodeChangingArgs) throws Exception {

    }
}
